#ifndef _lockex_H_
#define _lockex_H_

#include <util/util.h>

#if (TARGET_OS == OS_POSIX)
    #define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
    #define ATOM_CAS16(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
    #define ATOM_CAS_POINTER(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
    #define ATOM_INC(ptr) __sync_add_and_fetch(ptr, 1)
    #define ATOM_FINC(ptr) __sync_fetch_and_add(ptr, 1)
    #define ATOM_DEC(ptr) __sync_sub_and_fetch(ptr, 1)
    #define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1)
    #define ATOM_ADD(ptr,n) __sync_add_and_fetch(ptr, n)
    #define ATOM_SUB(ptr,n) __sync_sub_and_fetch(ptr, n)
    #define ATOM_AND(ptr,n) __sync_and_and_fetch(ptr, n)
#elif (TARGET_OS == OS_WINDOWS)
    #define ATOM_INC(ptr) InterlockedIncrement((ptr))
    #define ATOM_DEC(ptr) InterlockedDecrement((ptr))
    #define ATOM_ADD(ptr,n) InterlockedAdd(ptr, n)
    #define ATOM_AND(ptr,n) InterlockedAnd(ptr,n)
    #define ATOM_CAS_POINTER(ptr, oval, nval) InterlockedCompareExchangePointer(ptr, nval, oval)//log
    #define ATOM_CAS(ptr, oval, nval) (InterlockedCompareExchange(ptr, nval, oval)!=nval)
    #define ATOM_CAS16(ptr, oval, nval) (InterlockedCompareExchange16(ptr, nval, oval)!=nval)
    #define ATOM_FINC(ptr) InterlockedIncrement((ptr))
    #define ATOM_SUB(ptr,n) InterlockedExchangeAdd(ptr,-(ssize_t)(n)) 
#endif



inline static  long atomicadd(LONG* lpAddend) {
    return ATOM_ADD(lpAddend,1);
}

inline static  long atomicdel(long* lpAddend) {
    return ATOM_SUB(lpAddend,1);
}

inline static  void atomicinit(long* lpAddend) {
    ATOM_INC(lpAddend);
}

inline static  void atomicuninit(long* lpAddend) {
    ATOM_INC(lpAddend);
}

#endif